package com.example.highway.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.InspectorAdminDao;
import com.example.highway.dao.InspectorDao;
import com.example.highway.dao.OwnerDao;
import com.example.highway.entity.Inspector;
import com.example.highway.entity.InspectorAdmin;
import com.example.highway.entity.Owner;
import com.example.highway.retJson.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;

/*
 *   登录/注册接口
 */

@RestController
@CrossOrigin
public class RLController {

    @Autowired
    InspectorDao inspectorDao;
    @Autowired
    OwnerDao ownerDao;
    @Autowired
    InspectorAdminDao inspectorAdminDao;
    //127.0.0.1:8080/login
    @ResponseBody
    @RequestMapping("/login" )
    public MyJson login(@RequestBody String msg){
        JSONObject jsonObject = JSONObject.parseObject(msg);
        String userid = jsonObject.getString("UserName");
        String password = jsonObject.getString("PassWord");
        String identity = jsonObject.getString("value");
        //数据库访问
        if(identity.equalsIgnoreCase("2")){
            Optional<Inspector> o = inspectorDao.findById(userid);
            if(!o.isPresent()){
                return new MyJson("1",null);
            }
            String pwd = o.get().getPassword();
            JSONObject jo = new JSONObject();
            if(pwd.equals(password)){
                jo.put("status",0);
                jo.put("Identity",identity);
                return new MyJson("0",jo);
            }
        }else if(identity.equalsIgnoreCase("1")){
            Optional<Owner> o = ownerDao.findById(userid);
            if(!o.isPresent()){
                return new MyJson("1",null);
            }
            String pwd = o.get().getPassword();
            JSONObject jo = new JSONObject();
            if(pwd.equals(password)){
                jo.put("status",0);
                jo.put("Identity",identity);
                return new MyJson("0",jo);
            }
        }else if(identity.equalsIgnoreCase("0")){
            Optional<InspectorAdmin> o = inspectorAdminDao.findById(userid);
            if(!o.isPresent()){
                return new MyJson("1",null);
            }
            String pwd = o.get().getPassword();
            JSONObject jo = new JSONObject();
            if(pwd.equals(password)){
                jo.put("status",0);
                jo.put("Identity",identity);
                return new MyJson("0",jo);
            }
        }
        //登陆失败
        return new MyJson("1",null);


    }
    //127.0.0.1:8080/register
//    @ResponseBody
//    @RequestMapping("/register" )
//    public RegisterJson register(@RequestBody JSONObject jsonObject){
//        RegisterJson ret = new RegisterJson();
//        String userid = jsonObject.getString("userid");
//        String password = jsonObject.getString("pass");
//        String identity = jsonObject.getString("identity");
//        String username =jsonObject.getString("username");
//        int state=1;//表示未删除
//        //身份
//        int id=Integer.parseInt(identity);
//        try{
//            String sql = "SELECT * FROM user WHERE user_id='"+userid+"'";
//            ResultSet rs = MySQLTool.stat.executeQuery(sql);
//            if (rs.next()){
//                ret.setStatus(1);
//                ret.setMessage("该账号已存在");
//                return ret;
//            }
//            sql = "INSERT INTO user VALUES ('"+username+"','"+password+"','"+userid+"','"+state+"','"+id+"')";
//            MySQLTool.stat.execute(sql);
//            ret.setStatus(0);
//            ret.setMessage("注册成功");
//            return ret;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        ret.setStatus(1);
//        ret.setMessage("注册失败");
//        return ret;
//    }


}